eliminate queues in inifile. (#239)
authortsteven4 <tsteven4@users.noreply.github.com>
Thu, 2 Aug 2018 01:21:18 +0000 (19:21 -0600)
committerGitHub <noreply@github.com>
Thu, 2 Aug 2018 01:21:18 +0000 (19:21 -0600)
* eliminate queues in inifile replacing them with QHashs

* replace use of gbfile in inifile with QTextStream

bcr.cc
filter_vecs.cc
garmin_fs.cc
garmin_txt.cc
ggv_ovl.cc
inifile.cc
inifile.h
raymarine.cc
vecs.cc

diff --git a/bcr.cc b/bcr.cc
index 5673327c5eacc5568f74807651eeb86d6fd33beb..7840a480d337679b80abc08f52f29a8641bb3c4c 100644 (file)
--- a/bcr.cc
+++ b/bcr.cc
 */
 
 #include "defs.h"
-#include "cet_util.h"
 #include "csv_util.h"
 #include "garmin_tables.h"
 #include "inifile.h"
+#include <QtCore/QString>
 #include <cmath>
 #include <cstdio>
 #include <cstdlib>
@@ -187,9 +187,6 @@ static void
 bcr_rd_init(const QString& fname)
 {
   ini = inifile_init(fname, MYNAME);
-  if (ini->unicode) {
-    cet_convert_init(CET_CHARSET_UTF8, 1);
-  }
   bcr_init_radius();
 }
 
@@ -245,11 +242,12 @@ bcr_mercator_to_wgs84(const int north, const int east, double* lat, double* lon)
 static void
 bcr_data_read()
 {
-  char* str;
+  QString str;
 
   route_head* route = route_head_alloc();
 
-  if ((str = inifile_readstr(ini, "client", "routename"))) {
+  str = inifile_readstr(ini, "client", "routename");
+  if (!str.isNull()) {
     route->rte_name = str;
   }
 
@@ -258,15 +256,16 @@ bcr_data_read()
   for (int index = 1; index > 0; index ++) {
 
     char station[32];
-    char* str;
+    QString str;
     int mlat, mlon;            /* mercator data */
 
     snprintf(station, sizeof(station), "STATION%d", index);
-    if (nullptr == (str = inifile_readstr(ini, "coordinates", station))) {
+    str = inifile_readstr(ini, "coordinates", station);
+    if (str.isNull()) {
       break;
     }
 
-    if (2 != sscanf(str, "%d,%d", &mlon, &mlat)) {
+    if (2 != sscanf(CSTR(str), "%d,%d", &mlon, &mlat)) {
       fatal(MYNAME ": structure error at %s (Coordinates)!\n", station);
     }
 
@@ -275,32 +274,24 @@ bcr_data_read()
     wpt->shortname = station;
     bcr_mercator_to_wgs84(mlat, mlon, &wpt->latitude, &wpt->longitude);
 
-    if (nullptr != (str = inifile_readstr(ini, "client", station))) {
-      char* cx = strchr(str, ',');
-      if (cx == nullptr) {
+    str = inifile_readstr(ini, "client", station);
+    if (!str.isNull()) {
+      int cx = str.indexOf(',');
+      if (cx < 0) {
         fatal(MYNAME ": structure error at %s (Client)!\n", station);
       }
-      *cx++ = '\0';
-      bcr_handle_icon_str(str, wpt);
+      bcr_handle_icon_str(CSTR(str.left(cx)), wpt);
     }
 
-    if (nullptr != (str = inifile_readstr(ini, "description", station))) {
-      char* c = strchr(str, ',');
-      if (c != nullptr) {
-        *c = '\0';
-      }
-      if (*str) {
-        wpt->notes = str;
+    str = inifile_readstr(ini, "description", station);
+    if (!str.isNull()) {
+      QString note = str.section(',', 0, 0);
+      if (!note.isEmpty()) {
+        wpt->notes = note;
       }
-      if ((str = c)) {
-        str++;
-        c = strchr(str, ',');
-        if (c != nullptr) {
-          *c = '\0';
-        }
-        if (*str) {
-          wpt->shortname = str;
-        }
+      QString shortname = str.section(',', 1, 1);
+      if (!shortname.isEmpty()) {
+        wpt->shortname = shortname;
       }
     }
 
index 1bf3342b3f390f0080e8f360bc655c1528af2a53..23e0443f582edd3a9e77dfd7a00cf53534d708dd 100644 (file)
@@ -42,7 +42,7 @@
 #include "validate.h"
 #include "gbversion.h"
 #include "inifile.h"
-#include <QtCore/QStringList>
+#include <QtCore/QString>
 #include <cstdio>
 #include <cstdlib>
 #include <cstdlib> // qsort
@@ -201,14 +201,15 @@ find_filter_vec(const char* const vecname, const char** opts)
     struct arglist* args = vec->vec->get_args();
     if (args) {
       for (ap = args; ap->argstring; ap++) {
-        const char* temp = inifile_readstr(global_opts.inifile, vec->name, ap->argstring);
-        if (temp == nullptr) {
-          temp = inifile_readstr(global_opts.inifile, "Common filter settings", ap->argstring);
+        QString qtemp = inifile_readstr(global_opts.inifile, vec->name, ap->argstring);
+        if (qtemp.isNull()) {
+          qtemp = inifile_readstr(global_opts.inifile, "Common filter settings", ap->argstring);
         }
-        if (temp == nullptr) {
-          temp = ap->defaultvalue;
+        if (qtemp.isNull()) {
+          assign_option(vec->name, ap, ap->defaultvalue);
+        } else {
+          assign_option(vec->name, ap, CSTR(qtemp));
         }
-        assign_option(vec->name, ap, temp);
       }
     }
 
index fd7cae63b983dee1454bd42ba37e28e8d0d48fba..1986a75e1911e38be8eb97ec0a405490a1f83511 100644 (file)
@@ -26,6 +26,7 @@
 #include "garmin_tables.h"
 #include "inifile.h"
 
+#include <QtCore/QString>
 #include <QtCore/QXmlStreamWriter>
 #include <cassert>
 #include <cstdio>
@@ -387,8 +388,8 @@ garmin_fs_convert_category(const char* category_name, uint16_t* category)
       // warning: ā€˜%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=]
       assert((i>=0) && (i<16));
       snprintf(key, sizeof(key), "%d", i + 1);
-      char* c = inifile_readstr(global_opts.inifile, GMSD_SECTION_CATEGORIES, key);
-      if ((c != nullptr) && (case_ignore_strcmp(c, category_name) == 0)) {
+      QString c = inifile_readstr(global_opts.inifile, GMSD_SECTION_CATEGORIES, key);
+      if (c.compare(category_name, Qt::CaseInsensitive) == 0) {
         cat = (1 << i);
         break;
       }
index 0191288d4c6ebc13844300f2d0cffb5209c1bf22..2c7fdb641f598ec772bd015c0965fc8c315a5434 100644 (file)
@@ -32,6 +32,7 @@
 #include "jeeps/gpsmath.h"
 #include "strptime.h"
 
+#include <QtCore/QString>
 #include <cmath>
 #include <cstdlib> // qsort
 
@@ -388,8 +389,6 @@ print_date_and_time(const time_t time, const int time_only)
 static void
 print_categories(uint16_t categories)
 {
-  char* c;
-
   if (categories == 0) {
     return;
   }
@@ -397,21 +396,20 @@ print_categories(uint16_t categories)
   int count = 0;
   for (int i = 0; i < 16; i++) {
     if ((categories & 1) != 0) {
+      QString c;
       if (global_opts.inifile != nullptr) {
         char key[3];
         snprintf(key, sizeof(key), "%d", i + 1);
         c = inifile_readstr(global_opts.inifile, GMSD_SECTION_CATEGORIES, key);
-      } else {
-        c = nullptr;
       }
 
       gbfprintf(fout, "%s", (count++ > 0) ? "," : "");
-      if (c == nullptr) {
+      if (c.isNull()) {
         gbfprintf(fout, "Category %d", i+1);
       }
 //                             gbfprintf(fout, "%s", gps_categories[i]);
       else {
-        gbfprintf(fout, "%s", c);
+        gbfprintf(fout, "%s", CSTR(c));
       }
 
     }
index 263addefb04f9cea2b9d810450f9780fe610c8ef..a837598d09c721cc5aaf49ff8134c3631538d026 100644 (file)
  */
 
 #include "defs.h"
-#include "cet_util.h"
 #include "grtcirc.h"
 #include "inifile.h"
 
+#include <QtCore/QString>
 #include <cmath>
 #include <cstdio>
 #include <cstdlib>
@@ -83,9 +83,6 @@ static void
 ggv_ovl_rd_init(const QString& fname)
 {
   inifile = inifile_init(fname, MYNAME);
-  if (inifile->unicode) {
-    cet_convert_init(CET_CHARSET_UTF8, 1);
-  }
 
   route_ct = 0;
   track_ct = 0;
@@ -110,11 +107,12 @@ ggv_ovl_read()
     OVL_SYMBOL_TYP type = (OVL_SYMBOL_TYP) inifile_readint_def(inifile, symbol, "Typ", 0);
     int points = inifile_readint_def(inifile, symbol, "Punkte", -1);
 
+    QString lat;
+    QString lon;
     switch (type) {
 
       char coord[32];
       Waypoint* wpt;
-      char* cx;
       int group;
 
     case OVL_SYMBOL_LINE:
@@ -143,15 +141,17 @@ ggv_ovl_read()
           wpt = new Waypoint;
 
           snprintf(coord, sizeof(coord), "YKoord%d", j);
-          if ((cx = inifile_readstr(inifile, symbol, coord))) {
-            wpt->latitude = atof(cx);
+          lat = inifile_readstr(inifile, symbol, coord);
+          if (!lat.isNull()) {
+            wpt->latitude = lat.toDouble();
           } else {
             continue;
           }
 
           snprintf(coord, sizeof(coord), "XKoord%d", j);
-          if ((cx = inifile_readstr(inifile, symbol, coord))) {
-            wpt->longitude = atof(cx);
+          lon = inifile_readstr(inifile, symbol, coord);
+          if (!lon.isNull()) {
+            wpt->longitude = lon.toDouble();
           } else {
             continue;
           }
@@ -171,13 +171,15 @@ ggv_ovl_read()
       wpt = new Waypoint;
       wpt->shortname = symbol;
 
-      if ((cx = inifile_readstr(inifile, symbol, "YKoord"))) {
-        wpt->latitude = atof(cx);
+      lat = inifile_readstr(inifile, symbol, "YKoord");
+      if (!lat.isNull()) {
+        wpt->latitude = lat.toDouble();
       } else {
         continue;
       }
-      if ((cx = inifile_readstr(inifile, symbol, "XKoord"))) {
-        wpt->longitude = atof(cx);
+      lon = inifile_readstr(inifile, symbol, "XKoord");
+      if (!lon.isNull()) {
+        wpt->longitude = lon.toDouble();
       } else {
         continue;
       }
index 8885d8d165507ad600f70662f296efeca5c4d6bd..65b3300d92c743e319bc650bfd83504fa3e12a4b 100644 (file)
     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
 */
 
-#include "defs.h"
+#include "defs.h"              // for fatal, ugetenv, warning
 #include "inifile.h"
-#include <QtCore/QDir>
-#include <QtCore/QFile>
-#include <cstdio>
-#include <cstdlib>
+#include "src/core/file.h"     // for File
+#include <QtCore/QByteArray>   // for QByteArray
+#include <QtCore/QChar>        // for operator==, QChar
+#include <QtCore/QDir>         // for QDir
+#include <QtCore/QFile>        // for QFile
+#include <QtCore/QFileInfo>    // for QFileInfo
+#include <QtCore/QHash>        // for QHash
+#include <QtCore/QIODevice>    // for QIODevice::ReadOnly, QIODevice
+#include <QtCore/QTextStream>  // for QTextStream
+#include <QtCore/Qt>           // for CaseInsensitive
+#include <QtCore/QtGlobal>     // for qPrintable
 
 #define MYNAME "inifile"
 
-typedef struct inifile_entry_s {
-  queue Q;
-  char* key;
-  char* val;
-} inifile_entry_t;
+class InifileSection
+{
+public:
+  QString name;
+  QHash<QString, QString> entries;
 
-typedef struct inifile_section_s {
-  queue Q;
-  char* name;
-  int ientries;
-  queue entries;
-} inifile_section_t;
+  InifileSection() = default;
+  explicit InifileSection(QString nm) : name{nm} {}
+};
 
 /* internal procedures */
 
-#define START_BUFSIZE 257
-#define DELTA_BUFSIZE 128
-
 #define GPSBABEL_INIFILE "gpsbabel.ini"
 #define GPSBABEL_SUBDIR ".gpsbabel"
 
-/* Remember the filename we used so we can include it in errors. */
-static QString gbinipathname;
 
 static QString
 find_gpsbabel_inifile(const QString& path)  /* can be empty or NULL */
@@ -61,18 +60,18 @@ find_gpsbabel_inifile(const QString& path)  /* can be empty or NULL */
   return QFile(inipath).open(QIODevice::ReadOnly) ? inipath : QString();
 }
 
-static gbfile*
+static QString
 open_gpsbabel_inifile()
 {
-  gbfile* res = nullptr;
+  QString res;
 
   QString envstr = ugetenv("GPSBABELINI");
   if (!envstr.isNull()) {
     if (QFile(envstr).open(QIODevice::ReadOnly)) {
-      return gbfopen(envstr, "r", "GPSBabel");
+      return envstr;
     }
     warning("WARNING: GPSBabel-inifile, defined in environment, NOT found!\n");
-    return nullptr;
+    return res;
   }
   QString name = find_gpsbabel_inifile("");  // Check in current directory first.
   if (name.isNull()) {
@@ -80,117 +79,83 @@ open_gpsbabel_inifile()
     // Use &&'s early-out behaviour to try successive file locations: first
     // %APPDATA%, then %WINDIR%, then %SYSTEMROOT%.
     (name = find_gpsbabel_inifile(ugetenv("APPDATA"))).isNull()
-        && (name = find_gpsbabel_inifile(ugetenv("WINDIR"))).isNull()
-        && (name = find_gpsbabel_inifile(ugetenv("SYSTEMROOT"))).isNull();
+    && (name = find_gpsbabel_inifile(ugetenv("WINDIR"))).isNull()
+    && (name = find_gpsbabel_inifile(ugetenv("SYSTEMROOT"))).isNull();
 #else
     // Use &&'s early-out behaviour to try successive file locations: first
     // ~/.gpsbabel, then /usr/local/etc, then /etc.
     (name = find_gpsbabel_inifile(QDir::home().filePath(GPSBABEL_SUBDIR))).
-            isNull()
-        && (name = find_gpsbabel_inifile("/usr/local/etc")).isNull()
-        && (name = find_gpsbabel_inifile("/etc")).isNull();
+    isNull()
+    && (name = find_gpsbabel_inifile("/usr/local/etc")).isNull()
+    && (name = find_gpsbabel_inifile("/etc")).isNull();
 #endif
   }
   if (!name.isNull()) {
-    res = gbfopen(name, "r", "GPSBabel");
-    gbinipathname = name;
+    res = name;
   }
   return res;
 }
 
 static void
-inifile_load_file(gbfile* fin, inifile_t* inifile, const char* myname)
+inifile_load_file(QTextStream* stream, inifile_t* inifile, const char* myname)
 {
-  char* buf;
-  inifile_section_t* sec = nullptr;
-  int line = 0;
+  QString buf;
+  InifileSection section;
 
-  while ((buf = gbfgetstr(fin))) {
-    char* cin = lrtrim(buf);
-
-    if ((line++ == 0) && fin->unicode) {
-      inifile->unicode = 1;
-    }
+  while (!(buf = stream->readLine()).isNull()) {
+    buf = buf.trimmed();
 
-    if (*cin == '\0') {
+    if (buf.isEmpty()) {
       continue;  /* skip empty lines */
     }
-    if ((*cin == '#') || (*cin == ';')) {
+    if ((buf.at(0) == '#') || (buf.at(0) == ';')) {
       continue;  /* skip comments */
     }
 
-    if (*cin == '[') {
-
-      char* cend = strchr(++cin, ']');
-
-      if (cend != nullptr) {
-        *cend = '\0';
-        cin = lrtrim(cin);
+    if (buf.at(0) == '[') {
+      QString section_name;
+      if (buf.contains(']')) {
+        section_name = buf.mid(1, buf.indexOf(']') - 1).trimmed();
       }
-      if ((*cin == '\0') || (cend == nullptr)) {
-        fatal("%s: invalid section header '%s' in '%s'.\n", myname, cin,
-              qPrintable(gbinipathname));
+      if (section_name.isEmpty()) {
+        fatal("%s: invalid section header '%s' in '%s'.\n", myname, qPrintable(section_name),
+              qPrintable(inifile->source));
       }
 
-      sec = (inifile_section_t*) xcalloc(1, sizeof(*sec));
-
-      sec->name = xstrdup(cin);
-      QUEUE_INIT(&sec->entries);
-      ENQUEUE_TAIL(&inifile->secs, &sec->Q);
-      inifile->isecs++;
+      // form lowercase key to implement CaseInsensitive matching.
+      section_name = section_name.toLower();
+      inifile->sections.insert(section_name, InifileSection(section_name));
+      section = inifile->sections.value(section_name);
     } else {
-      if (sec == nullptr) {
+      if (section.name.isEmpty()) {
         fatal("%s: missing section header in '%s'.\n", myname,
-              qPrintable(gbinipathname));
+              qPrintable(inifile->source));
       }
 
-      inifile_entry_t* entry = (inifile_entry_t*) xcalloc(1, sizeof(*entry));
-      ENQUEUE_TAIL(&sec->entries, &entry->Q);
-      sec->ientries++;
-
-      char* cx = strchr(cin, '=');
-      if (cx != nullptr) {
-        *cx = '\0';
-        cin = lrtrim(cin);
-      }
-
-      entry->key = xstrdup(cin);
-
-      if (cx != nullptr) {
-        cx = lrtrim(++cx);
-        entry->val = xstrdup(cx);
-      } else {
-        entry->val = xstrdup("");
-      }
+      // Store key in lower case to implement CaseInsensitive matching.
+      QString key = buf.section('=', 0, 0).trimmed().toLower();
+      // Take some care so the return from inifile_find_value can
+      // be used to distinguish between a key that isn't found
+      // and a found key without a value, i.e. force value
+      // to be non-null but possibly empty.
+      QString value = buf.section('=', 1).append("").trimmed();
+      section.entries.insert(key, value);
+
+      // update the QHash sections with the modified InifileSection section.
+      inifile->sections.insert(section.name, section);
     }
   }
 }
 
-static char*
-inifile_find_value(const inifile_t* inifile, const char* sec_name, const char* key)
+static const QString
+inifile_find_value(const inifile_t* inifile, const QString& sec_name, const QString& key)
 {
-  queue* elem, *tmp;
-
   if (inifile == nullptr) {
-    return nullptr;
+    return QString();
   }
 
-  QUEUE_FOR_EACH(&inifile->secs, elem, tmp) {
-    inifile_section_t* sec = reinterpret_cast<inifile_section_t *>(elem);
-
-    if (case_ignore_strcmp(sec->name, sec_name) == 0) {
-      queue* elem, *tmp;
-
-      QUEUE_FOR_EACH(&sec->entries, elem, tmp) {
-        inifile_entry_t* entry = reinterpret_cast<inifile_entry_t *>(elem);
-
-        if (case_ignore_strcmp(entry->key, key) == 0) {
-          return entry->val;
-        }
-      }
-    }
-  }
-  return nullptr;
+  // CaseInsensitive matching implemented by forcing sec_name & key to lower case.
+  return inifile->sections.value(sec_name.toLower()).entries.value(key.toLower());
 }
 
 /* public procedures */
@@ -205,86 +170,51 @@ inifile_find_value(const inifile_t* inifile, const char* sec_name, const char* k
 inifile_t*
 inifile_init(const QString& filename, const char* myname)
 {
-  gbfile* fin = nullptr;
+  QString name;
 
   if (filename.isEmpty()) {
-    fin = open_gpsbabel_inifile();
-    if (fin == nullptr) {
+    name = open_gpsbabel_inifile();
+    if (name.isEmpty()) {
       return nullptr;
     }
   } else {
-    fin = gbfopen(filename, "rb", myname);
+    name = filename;
   }
 
-  inifile_t* result = (inifile_t*) xcalloc(1, sizeof(*result));
-  QUEUE_INIT(&result->secs);
-  inifile_load_file(fin, result, myname);
+  gpsbabel::File file(name);
+  file.open(QFile::ReadOnly);
+  QTextStream stream(&file);
+  stream.setCodec("UTF-8");
+  stream.setAutoDetectUnicode(true);
+
+  auto* result = new inifile_t;
+  QFileInfo fileinfo(file);
+  result->source = fileinfo.absoluteFilePath();
+  inifile_load_file(&stream, result, myname);
 
-  gbfclose(fin);
+  file.close();
   return result;
 }
 
 void
 inifile_done(inifile_t* inifile)
 {
-  if (inifile == nullptr) {
-    return;
-  }
-
-  if (inifile->isecs > 0) {
-    queue* elem, *tmp;
-
-    QUEUE_FOR_EACH(&inifile->secs, elem, tmp) {
-      inifile_section_t* sec = reinterpret_cast<inifile_section_t *>(elem);
-
-      if (sec->ientries > 0) {
-        queue* elem, *tmp;
-
-        QUEUE_FOR_EACH(&sec->entries, elem, tmp) {
-          inifile_entry_t* entry = reinterpret_cast<inifile_entry_t *>(elem);
-
-          if (entry->key) {
-            xfree(entry->key);
-          }
-          if (entry->val) {
-            xfree(entry->val);
-          }
-          dequeue(elem);
-          xfree(entry);
-        }
-      }
-      dequeue(elem);
-      if (sec->name) {
-        xfree(sec->name);
-      }
-      xfree(sec);
-    }
-    xfree(inifile);
-  }
-  gbinipathname.clear();
+  delete inifile;
 }
 
-int
+bool
 inifile_has_section(const inifile_t* inifile, const char* section)
 {
-  queue* elem, *tmp;
-
-  QUEUE_FOR_EACH(&inifile->secs, elem, tmp) {
-    inifile_section_t* sec = reinterpret_cast<inifile_section_t *>(elem);
-    if (case_ignore_strcmp(sec->name, section) == 0) {
-      return 1;
-    }
-  }
-  return 0;
+  return inifile->sections.contains(QString(section).toLower());
 }
 
 /*
      inifile_readstr:
-       returns NULL if not found, otherwise a pointer to the value of key ...
-       all key values are valid entities until "inifile_done"
+       returns a null QString if not found, otherwise a non-null but possibly
+       empty Qstring with the value of key ...
  */
 
-char*
+QString
 inifile_readstr(const inifile_t* inifile, const char* section, const char* key)
 {
   return inifile_find_value(inifile, section, key);
@@ -299,14 +229,14 @@ inifile_readstr(const inifile_t* inifile, const char* section, const char* key)
 int
 inifile_readint(const inifile_t* inifile, const char* section, const char* key, int* value)
 {
-  char* str = inifile_find_value(inifile, section, key);
+  const QString str = inifile_find_value(inifile, section, key);
 
-  if (str == nullptr) {
+  if (str.isNull()) {
     return 0;
   }
 
   if (value != nullptr) {
-    *value = atoi(str);
+    *value = str.toInt();
   }
   return 1;
 }
index 8fc2bda3d0a9807f5241fff6914b3d21410c44fc..911633192ecade170065fa1ef445355554de1226 100644 (file)
--- a/inifile.h
+++ b/inifile.h
 #ifndef HAVE_INIFILE_H
 #define HAVE_INIFILE_H
 
-#include "defs.h"
+#include <QtCore/QHash>    // for QHash
+#include <QtCore/QList>    // for QList
+#include <QtCore/QString>  // for QString
 
-typedef struct inifile_s {
-  int isecs;                   /* number of sections */
-  queue secs;                  /* sections */
-  uint8_t unicode:1;
-} inifile_t;
+class InifileSection;
+struct inifile_t {
+  QHash<QString, InifileSection> sections;
+  QString source;
+};
 
 /*
        inifile_init:
@@ -37,14 +39,14 @@ typedef struct inifile_s {
 inifile_t* inifile_init(const QString& filename, const char* myname);
 void inifile_done(inifile_t* inifile);
 
-int inifile_has_section(const inifile_t* inifile, const char* section);
+bool inifile_has_section(const inifile_t* inifile, const char* section);
 
 /*
      inifile_readstr:
-       returns NULL if not found, otherwise a pointer to the value of key ...
-       all key values are valid entities until "inifile_done"
+       returns a null QString if not found, otherwise a non-null but possibly
+       empty Qstring with the value of key ...
  */
-char* inifile_readstr(const inifile_t* inifile, const char* section, const char* key);
+QString inifile_readstr(const inifile_t* inifile, const char* section, const char* key);
 
 /*
      inifile_readint:
index ae6e1872e62153d83771496f156cf302524e90f4..2093519cad4bea4ad7ac21b05848b5cd24015e2c 100644 (file)
 */
 
 #include "defs.h"
-#include "cet_util.h"
 #include "csv_util.h"
 #include "inifile.h"
 
+#include <QtCore/QString>
 #include <cctype>
 #include <cstdio>
 #include <cstdlib>
@@ -167,9 +167,6 @@ static void
 raymarine_rd_init(const QString& fname)
 {
   fin = inifile_init(fname, MYNAME);
-  if (fin->unicode) {
-    cet_convert_init(CET_CHARSET_UTF8, 1);
-  }
 }
 
 static void
@@ -185,37 +182,43 @@ raymarine_read()
 
   for (unsigned int ix = 0; ix < 0x3FFF; ix++) {
     char sect[10];
-    char* str, *name, *lat, *lon;
+    QString str, name, lat, lon;
 
     /* built section identifier */
     snprintf(sect, sizeof(sect), "Wp%d", ix);
 
     /* try to read our most expected values */
-    if (nullptr == (name = inifile_readstr(fin, sect, "Name"))) {
+    name = inifile_readstr(fin, sect, "Name");
+    if (name.isNull()) {
       break;
     }
-    if (nullptr == (lat = inifile_readstr(fin, sect, "Lat"))) {
+    lat = inifile_readstr(fin, sect, "Lat");
+    if (lat.isNull()) {
       break;
     }
-    if (nullptr == (lon = inifile_readstr(fin, sect, "Long"))) {
+    lon = inifile_readstr(fin, sect, "Long");
+    if (lon.isNull()) {
       break;
     }
 
     Waypoint* wpt = new Waypoint;
     wpt->shortname = name;
-    wpt->latitude = atof(lat);
-    wpt->longitude = atof(lon);
+    wpt->latitude = lat.toDouble();
+    wpt->longitude = lon.toDouble();
     waypt_add(wpt);
 
     /* try to read optional values */
-    if (((str = inifile_readstr(fin, sect, "Notes"))) && *str) {
+    str = inifile_readstr(fin, sect, "Notes");
+    if (!str.isEmpty()) {
       wpt->notes = str;
     }
-    if (((str = inifile_readstr(fin, sect, "Time"))) && *str) {
-      wpt->SetCreationTime(EXCEL_TO_TIMET(atof(str)));
+    str = inifile_readstr(fin, sect, "Time");
+    if (!str.isEmpty()) {
+      wpt->SetCreationTime(EXCEL_TO_TIMET(str.toDouble()));
     }
-    if (((str = inifile_readstr(fin, sect, "Bmp"))) && *str) {
-      unsigned int symbol = atoi(str);
+    str = inifile_readstr(fin, sect, "Bmp");
+    if (!str.isEmpty()) {
+      unsigned int symbol = str.toInt();
 
       if ((symbol < 3) && (symbol >= RAYMARINE_SYMBOL_CT)) {
         symbol = RAYMARINE_STD_SYMBOL;
@@ -228,10 +231,11 @@ raymarine_read()
 
   for (unsigned int rx = 0; rx < 0x3FFF; rx++) {
     char sect[10];
-    char* name;
+    QString name;
 
     snprintf(sect, sizeof(sect), "Rt%d", rx);
-    if (nullptr == (name = inifile_readstr(fin, sect, "Name"))) {
+    name = inifile_readstr(fin, sect, "Name");
+    if (name.isNull()) {
       break;
     }
 
@@ -243,15 +247,15 @@ raymarine_read()
       char buff[32];
 
       snprintf(buff, sizeof(buff), "Mk%d", wx);
-      char* str = inifile_readstr(fin, sect, buff);
-      if ((str == nullptr) || (*str == '\0')) {
+      QString str = inifile_readstr(fin, sect, buff);
+      if (str.isEmpty()) {
         break;
       }
 
       Waypoint* wpt = find_waypt_by_name(str);
       if (wpt == nullptr)
         fatal(MYNAME ": No associated waypoint for route point %s (Route %s)!\n",
-              str, qPrintable(rte->rte_name));
+              qPrintable(str), qPrintable(rte->rte_name));
 
       route_add_wpt(rte, new Waypoint(*wpt));
     }
diff --git a/vecs.cc b/vecs.cc
index 4c4a947f5bd334a0a130ef48f3dcec21a3a9c4d3..f7cd6d15381ce17063a0f196e412d768c958c4ad 100644 (file)
--- a/vecs.cc
+++ b/vecs.cc
@@ -23,6 +23,7 @@
 #include "csv_util.h"
 #include "gbversion.h"
 #include "inifile.h"
+#include <QtCore/QString>
 #include <cstdio>
 #include <cstdlib> // qsort
 
@@ -1281,10 +1282,8 @@ find_vec(const char* vecname, const char** opts)
 
     if (vec->vec->args) {
       for (auto ap = vec->vec->args; ap->argstring; ap++) {
-        const char* opt;
-
         if (res) {
-          opt = get_option(*opts, ap->argstring);
+          const char* opt = get_option(*opts, ap->argstring);
           if (opt) {
             found = 1;
             assign_option(svecname, ap, opt);
@@ -1292,14 +1291,15 @@ find_vec(const char* vecname, const char** opts)
             continue;
           }
         }
-        opt = inifile_readstr(global_opts.inifile, vec->name, ap->argstring);
-        if (opt == nullptr) {
-          opt = inifile_readstr(global_opts.inifile, "Common format settings", ap->argstring);
+        QString qopt = inifile_readstr(global_opts.inifile, vec->name, ap->argstring);
+        if (qopt.isNull()) {
+          qopt = inifile_readstr(global_opts.inifile, "Common format settings", ap->argstring);
         }
-        if (opt == nullptr) {
-          opt = ap->defaultvalue;
+        if (qopt.isNull()) {
+          assign_option(vec->name, ap, ap->defaultvalue);
+        } else {
+          assign_option(vec->name, ap, CSTR(qopt));
         }
-        assign_option(vec->name, ap, opt);
       }
     }
     if (opts && opts[0] && !found) {
@@ -1338,10 +1338,8 @@ find_vec(const char* vecname, const char** opts)
 
     if (vec_list[0].vec->args) {
       for (auto ap = vec_list[0].vec->args; ap->argstring; ap++) {
-        const char* opt;
-
         if (res) {
-          opt = get_option(*opts, ap->argstring);
+          const char* opt = get_option(*opts, ap->argstring);
           if (opt) {
             found = 1;
             assign_option(svecname, ap, opt);
@@ -1349,14 +1347,15 @@ find_vec(const char* vecname, const char** opts)
             continue;
           }
         }
-        opt = inifile_readstr(global_opts.inifile, svec->name, ap->argstring);
-        if (opt == nullptr) {
-          opt = inifile_readstr(global_opts.inifile, "Common format settings", ap->argstring);
+        QString qopt = inifile_readstr(global_opts.inifile, svec->name, ap->argstring);
+        if (qopt.isNull()) {
+          qopt = inifile_readstr(global_opts.inifile, "Common format settings", ap->argstring);
         }
-        if (opt == nullptr) {
-          opt = ap->defaultvalue;
+        if (qopt.isNull()) {
+          assign_option(svec->name, ap, ap->defaultvalue);
+        } else {
+          assign_option(svec->name, ap, CSTR(qopt));
         }
-        assign_option(svec->name, ap, opt);
       }
     }